package smartlifev30.com.sdk.api.gateway_app;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import smartlifev30.com.sdk.bean.response.LoginGatewayResponse;
import smartlifev30.com.sdk.util.JsonUtil;
import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.api.ConstantGateway;
import smartlifev30.com.sdk.bean.request.GetUserPermissionRequest;
import smartlifev30.com.sdk.bean.request.LoginGatewayRequest;
import smartlifev30.com.sdk.bean.request.SetUserPermissionRequest;
import smartlifev30.com.sdk.bean.request.UserQueryRequest;
import smartlifev30.com.sdk.bean.response.LoginResponse;
import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.baiwei.ResponseHelper;

/**
 * Created by tys on 2018/8/13.
 */

public class UserMgmtGatewayImpl implements IUserMgmtGateway {

    @Override
    public void loginGatewayByToken(@NonNull String userName, @NonNull String token, @NonNull String sn,
                                    @NonNull CallBack<LoginGatewayResponse> callBack) {
        LoginGatewayRequest loginGatewayRequest = new LoginGatewayRequest();
        loginGatewayRequest.setFrom(userName);
        loginGatewayRequest.setTo(sn);
        loginGatewayRequest.setMsg_id(callBack.getMsgId());
        loginGatewayRequest.setMsg_class(ConstantGateway.MSG_CLASS_USER);
        loginGatewayRequest.setMsg_name("user_login");
        loginGatewayRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        loginGatewayRequest.setToken(token);
        ResponseHelper.addListen(callBack);
        NetManager.getInstance().sendMessage(JsonUtil.toString(loginGatewayRequest));
    }

    @Override
    public void loginGatewayByPwd(@NonNull String userName, @NonNull String pwd, @NonNull String sn,
                                  @NonNull CallBack<LoginGatewayResponse> callBack) {
        LoginGatewayRequest loginGatewayRequest = new LoginGatewayRequest();
        loginGatewayRequest.setFrom(userName);
        loginGatewayRequest.setTo(sn);
        loginGatewayRequest.setMsg_id(callBack.getMsgId());
        loginGatewayRequest.setMsg_class(ConstantGateway.MSG_CLASS_USER);
        loginGatewayRequest.setMsg_name("user_login");
        loginGatewayRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        loginGatewayRequest.setUser(loginGatewayRequest.new User(pwd));
        ResponseHelper.addListen(callBack);
        NetManager.getInstance().sendMessage(JsonUtil.toString(loginGatewayRequest));
    }

    @Override
    public void getUserPermission(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull String phoneNum, @NonNull CallBack<LoginResponse> callBack) {
        GetUserPermissionRequest getUserPermissionRequest = new GetUserPermissionRequest();
        getUserPermissionRequest.setFrom(userName);
        getUserPermissionRequest.setTo(sn);
        getUserPermissionRequest.setMsg_id(callBack.getMsgId());
        getUserPermissionRequest.setMsg_class(ConstantGateway.MSG_CLASS_USER);
        getUserPermissionRequest.setMsg_name("user_permission_get");
        getUserPermissionRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        getUserPermissionRequest.setToken(token);
        getUserPermissionRequest.setUser(getUserPermissionRequest.new User(phoneNum));
        ResponseHelper.addListen(callBack);
        NetManager.getInstance().sendMessage(JsonUtil.toString(getUserPermissionRequest));
    }

    @Override
    public void setUserPermission(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull String phoneNum,
                                  @NonNull Integer permission, @Nullable List<Integer> deviceList, @NonNull CallBack<LoginResponse> callBack) {
        SetUserPermissionRequest setUserPermissionRequest = new SetUserPermissionRequest();
        setUserPermissionRequest.setFrom(userName);
        setUserPermissionRequest.setTo(sn);
        setUserPermissionRequest.setMsg_id(callBack.getMsgId());
        setUserPermissionRequest.setMsg_class(ConstantGateway.MSG_CLASS_USER);
        setUserPermissionRequest.setMsg_name("user_permission_set");
        setUserPermissionRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        SetUserPermissionRequest.User user = setUserPermissionRequest.new User();
        user.setPhone_num(phoneNum);
        user.setPermission(permission);
        List<SetUserPermissionRequest.DeviceId> list = null;
        if (deviceList != null) {
            list = new ArrayList<>();
            for (Integer integer : deviceList) {
                list.add(setUserPermissionRequest.new DeviceId(integer));
            }
        }
        user.setDevice_list(list);
        setUserPermissionRequest.setUser(user);
        ResponseHelper.addListen(callBack);
        NetManager.getInstance().sendMessage(JsonUtil.toString(setUserPermissionRequest));
    }

    @Override
    public void usrQuery(@NonNull String userName, @NonNull String token, @NonNull String sn, @Nullable List<String> phoneNum,
                         @NonNull CallBack<LoginResponse> callBack) {
        UserQueryRequest userQueryRequest = new UserQueryRequest();
        userQueryRequest.setFrom(userName);
        userQueryRequest.setTo(sn);
        userQueryRequest.setMsg_id(callBack.getMsgId());
        userQueryRequest.setMsg_class(ConstantGateway.MSG_CLASS_USER);
        userQueryRequest.setMsg_name("user_query");
        userQueryRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        userQueryRequest.setUsers(userQueryRequest.new User(phoneNum));
        ResponseHelper.addListen(callBack);
        NetManager.getInstance().sendMessage(JsonUtil.toString(userQueryRequest));
    }


}
